﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Data;
using System.Reflection;
using System.Net.Mail;

namespace Utility
{
    public static class WebHelper
    {

        // Xuất thông báo
        public static void ShowMessage(string content, Page page)
        {
            ScriptManager.RegisterStartupScript(page, typeof(Page), "AlertScript",
                "alert('" + content + "');", true);
        }

        // Lấy giá trị của Label trong DataList
        public static String GetItemDataList(object sender, String controlName)
        {
            DataListItem item = ((Control)sender).NamingContainer as DataListItem;
            string value = (((Label)item.FindControl(controlName)).Text);
            return value;
        }

        // Tạo confirm cho Button
        public static void ConfirmButton(Button btn, String content)
        {
            btn.Attributes.Add("onclick",
                    "return confirm('" + content + "');");
        }

        // Lấy già trị trong GridView theo cột
        public static String GetIdGridView(GridView grid, int col, GridViewSelectEventArgs e)
        {
            return grid.Rows[e.NewSelectedIndex].Cells[col].Text;
        }

        // Lấy DataKey (khóa chính) trong GridView
        public static String GetDataKey(GridView grid, GridViewDeleteEventArgs e)
        {
            return grid.DataKeys[e.RowIndex].Value.ToString();
        }

        // Hiển thỉ các UserControl trong trang
        public static void LoadUserControl(Page page, string path)
        {
            Control myUserControl = (Control)
                 page.LoadControl("~/" + path);
            page.Master.FindControl("ContentPlaceHolder1").Controls.Add(myUserControl);
        }

        // Hiển thỉ dữ liệu lên Combobox
        public static void LoadItemDropdown(DataTable tb, DropDownList ddl, string name, string id)
        {
            foreach (DataRow dr in tb.Rows)
            {
                ddl.Items.Add(dr[name].ToString() + "-" + dr[id].ToString());
            }
        }

       
        // mã hóa psssword
        public static string GenerateMD5(string str)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] byteArray = Encoding.ASCII.GetBytes(str);

            byteArray = md5.ComputeHash(byteArray);

            string hashedValue = "";

            foreach (byte b in byteArray)
            {
                hashedValue += b.ToString("x2");
            }

            return hashedValue;
        }

        // so sánh 2 chuỗi có giống nhau hay ko
        public static int VerifyString(string s1, string s2)
        {
            return string.Compare(s1,s2);
        }

        // confirm khi xóa dữ liệu trong gridview
        public static void ConfirmDelete(String content, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.DataRow) return;

            int lastCellIndex = e.Row.Cells.Count - 1;
            LinkButton deleteButton = (LinkButton)e.Row.Cells[lastCellIndex].Controls[0];
            deleteButton.OnClientClick =
              "if (!window.confirm('" + content + "')) return false;";
        }


        public static void MergeRows(GridView gridView)
        {
            for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--)
            {
                GridViewRow row = gridView.Rows[rowIndex];
                GridViewRow previousRow = gridView.Rows[rowIndex + 1];

                for (int i = 0; i < row.Cells.Count; i++)
                {
                    if (row.Cells[i].Text == previousRow.Cells[i].Text)
                    {
                        row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : previousRow.Cells[i].RowSpan + 1;
                        previousRow.Cells[i].Visible = false;
                    }
                }
            }
        }

        //protected void gridView_PreRender(object sender, EventArgs e)
        //{
        //    GridDecorator.MergeRows(gridView);
        //}

        //========================Phân Trang trong GridView===================================


        static int stt = 1;
        private static string get_stt()
        {
            return (Convert.ToString(stt++));
        }

        public static void pageIndexChanging(GridView grid, GridViewPageEventArgs e)
        {
            grid.PageIndex = e.NewPageIndex;
            int trang_thu = e.NewPageIndex;
            int so_dong = grid.PageSize;

            stt = trang_thu * so_dong + 1;
        }


    }
}

